package ar.com.globallogic.promomr.api.converters;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ar.com.globallogic.promomr.api.resources.PublicationResponse;
import ar.com.globallogic.promomr.domain.PendingState;
import ar.com.globallogic.promomr.domain.Publication;

public class PublicationConverter {
	
	public static PublicationResponse convert(Publication publication){
		PublicationResponse response = new PublicationResponse();
		response.setId(publication.getId());
		response.setState(publication.getState().getName());
		if(response.getState().equals("PENDING")){
			Date publicationDate = ((PendingState) publication.getState()).getPublicationDate();
			response.setPublicationDate(publicationDate);
		}
		response.setContentType(publication.getContent().getClass().getSimpleName());
		response.setContentId(publication.getContent().getId());
		response.setContentName(publication.getContent().getName());
		response.setContentDescription(publication.getContent().getDescription());
		response.setContentResource(publication.getContent().getUrl());
		response.setUserName(publication.getOwner().getUserName());
		return response;
	}
	
	public static List<PublicationResponse> convert(List<Publication> publications){
		List<PublicationResponse> responses = new ArrayList<PublicationResponse>();
		for (Publication publication : publications) {
			responses.add(convert(publication));
		}
		return responses;
	}
}
